import {defineStore} from 'pinia'
import {ElMessage} from "element-plus";
import {getConversionId} from "@/views/system/ai/js/AiConversationRequest.js";

// 定义用户状态管理store
export const useAiSessionStore = defineStore('ai-session', {
    // 持久化配置
    persist: {
        key: 'ai-session',
        storage: sessionStorage,
        paths: ['bubbles', 'conversionId'] // 只持久化userInfo和token字段
    },

    // 状态定义
    state: () => ({
        bubbles: [], // 用户信息对象
        conversionId: null, // 用户信息对象
        sendLoading: false,
        source: null,
    }),

    // 计算属性
    getters: {},

    // 方法定义
    actions: {
        setConversationId(conversationId) {
            if (this.conversionId === null) {
                this.conversionId = conversationId
                this.sseInit(conversationId)
                console.log('首次请求开启了 sse 连接。。。')
            }
        },
        clearBubbles() {
            this.bubbles = []
        },
        startNewConversation() {
            getConversionId().then(res => {
                if (res.success) {
                    this.clearBubbles()
                    this.conversionId = res.data
                    this.sseInit(res.data)
                }
            }).catch(err => {
                ElMessage.error('服务器错误')
            })

        },
        sseInit(conversionId) {
            if (this.source != null) {
                return
            }
            if (window.EventSource) {
                console.log('sse 初始化正常... 开始建立连接')
                this.source = new EventSource('http://localhost:8100/aiChat/doConnect?conversationId=' + conversionId)
                this.source.addEventListener('open', () => {
                    console.log('sse链接建立open...')
                }, false);

                this.source.addEventListener('message', (e) => {
                    console.log('sse链接建立 message...', e.data)
                }, false);

                this.source.addEventListener('add', (e) => {
                    console.log('sse 数据开始追加 add...')
                    console.log(e)
                    this.bubbles[this.bubbles.length - 1].content += e.data
                }, false);

                this.source.addEventListener('done', () => {
                    console.log('sse完成...')
                }, false);

                this.source.addEventListener('finish', () => {
                    console.log('sse链接完成推送...')
                }, false);


                this.source.addEventListener('start', () => {
                    console.log('sse 数据传输开始 start...')
                    this.bubbles[this.bubbles.length - 1].loading = false
                }, false);

                this.source.onmessage = e => {
                    if (e.data === '') return;   // 忽略空数据
                    console.log(e.data);
                };

                this.source.addEventListener('done', () => {
                    console.log('sse 数据传输完毕 done...')
                    this.bubbles[this.bubbles.length - 1].typing = false
                    this.sendLoading = false
                }, false);

            } else {
                ElMessage.error('你当前的浏览器不支持 SSE 连接...')
            }
            return this.source
        }
    }
})